import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025-04-04
 * Time: 19:15
 */
public class Solution {
    //滑动窗口
    public List<Integer> findAnagrams3(String s, String p){
        int sLen = s.length();
        int pLen = p.length();
        List<Integer> ret = new ArrayList<>();
        if (sLen < pLen) return ret;
        int[] hash1 = new int[26];//统计字符串p每个字符出现的次数
        for (int i = 0; i < pLen; i++) {
            hash1[p.charAt(i) - 'a']++;
        }
        int[] hash2 = new int[26];//统计窗口每个字符出现的次数
        for (int left = 0,right = 0,count = 0; right < sLen; right++) {
            int in = s.charAt(right) - 'a';
            hash2[in]++;//进窗口
            if (hash2[in] <= hash1[in]){//维护count
                count++;
            }
            while(right - left + 1 > pLen){//出窗口
                int out = s.charAt(left) - 'a';
                if (hash2[out] <= hash1[out]){
                    count--;
                }
                hash2[out]--;
                left++;
            }
            if (count == pLen){
                ret.add(left);
            }
        }
        return ret;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        List<Integer> ret = solution.findAnagrams3("baa", "aa");
        for (Integer cur : ret){
            System.out.println(cur);
        }
    }
}